//
//  NSDate+Time.m
//  LYProfiles
//
//  Created by boom on 2017/6/8.
//  Copyright © 2017年 boomLi. All rights reserved.
//

#import "NSDate+Time.h"

@implementation NSDate (Time)

/**根据字符串返回的时间戳*/
+(NSString *)getTimeStampFromDate:(NSString *)dateStr  Style:(NSString *)style{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:style];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSDate * date = [formatter dateFromString:dateStr];
    return [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
}

/**根据日期返回时间戳*/
+(NSString *)getTimeStampFromNSDate:(NSDate *)date Style:(NSString *)style{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:style];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSString * dateStr = [formatter stringFromDate:date];
    NSDate * dateNow = [formatter dateFromString:dateStr];
    return [NSString stringWithFormat:@"%ld", (long)[dateNow timeIntervalSince1970]];
}

/**根据时间戳返回日期*/
+(NSDate *)getNSDateByStamp:(NSString *)stamp Style:(NSString *)style{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:style];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:stamp.integerValue];
    NSString * dateStr = [formatter stringFromDate:date];
    return [formatter dateFromString:dateStr];
}

/**根据时间戳返回日期*/
+(NSString *)getNSDateStringByStamp:(NSString *)stamp Style:(NSString *)style{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:style];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:stamp.integerValue];
    return [formatter stringFromDate:date];
}

/**判断是否是周末*/
+(BOOL)isOnTheWeakend:(NSString *)dateStr{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    [formatter setTimeZone:[NSTimeZone systemTimeZone]];
    NSLocale* local =[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale: local];
    NSDate *date = [formatter dateFromString:dateStr];
    if (!date) return NO;
    NSCalendar * calendar = [NSCalendar currentCalendar];
    NSInteger week = [[calendar components:NSCalendarUnitWeekday fromDate:date] weekday];
    if (week == 7) {
        return YES;
    }
    return NO;

}


@end
